package com.taptech.doufu.ui.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.constant.HomeEnterType;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.constant.IntentKey;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.listener.IListScollerState;
import com.taptech.doufu.manager.UserLogManager;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.UserMemberActivity;
import com.taptech.doufu.ui.view.AutoPollAdapter;
import com.taptech.doufu.ui.view.AutoPollRecyclerView;
import com.taptech.doufu.ui.view.choice.ChoiceUtils;
import com.taptech.doufu.ui.view.viewflow.view.CircleFlowIndicator;
import com.taptech.doufu.ui.view.viewflow.view.HomeViewFlow;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.ui.adapter.DfhomeNovelAdapter3;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.DFHomeCategoryBean;
import com.taptech.doufu.bean.DFHomeNovelBeans;
import com.taptech.doufu.bean.MarketNovelNoticeBean;
import com.taptech.doufu.bean.novel.PersonalLikeBean;
import com.taptech.doufu.bean.novel.PersonalLikeGsonBean;
import com.taptech.doufu.util.CommonUtils;
import com.taptech.doufu.constant.RecycleViewLoadType;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.ToUpOrDownable;
import com.taptech.doufu.presenter.home.HomeBannerPresenter;
import com.taptech.doufu.presenter.home.HomeCategoryPresenter;
import com.taptech.doufu.presenter.home.HomeSpeakerPresenter;
import com.taptech.doufu.presenter.home.HomeTopicPresenter;
import com.taptech.doufu.ui.activity.NovelRankActivity;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.StartActivityUtils;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.ui.view.HomeBannerImageAdapter;
import com.taptech.doufu.ui.view.NetworkImageView;
import com.taptech.doufu.ui.view.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 彩虹页面
 */
public class DFBLNovelFragment extends HomeBaseFragment
        implements PullToRefreshListView.OnLoadAndRefreshListener,
        HomeTopicPresenter.OnHomeTopicView,
        HomeSpeakerPresenter.OnHomeSpeakerView,
        HomeBannerPresenter.OnHomeBannerView ,
        HomeCategoryPresenter.OnHomeCategoryView{
    private PullToRefreshListView listView;
    private HomeViewFlow viewFlow;
    private HomeBannerImageAdapter viewFlowAdapter;//轮播图适配器
    private CircleFlowIndicator flowIndicator;
    private RelativeLayout line1, line2, line3, line4, line5, line6,line7,line8,line9,line10;
    LinearLayout line_bg, llBroadcast, llAutoLayout,layBottomLine;
    private ImageView noticeImg;
    private NetworkImageView[] classifyImag = new NetworkImageView[10];
    private TextView[] classifyName = new TextView[10];
    private List<DFHomeBannerBean> bannerList;
    private List<DFHomeCategoryBean> categoryList;
    private List<Object> contentAdaList;
    private TTHomeViewPager viewPager;//父类所含有的viewpage，用来控制图片轮转时，触摸事件不回传给父类
    private DfhomeNovelAdapter3 listAda;//滑动适配器
    private String last = "";
    private AutoPollRecyclerView autoPollRecyclerView;
    private List<MarketNovelNoticeBean> listNotice;
    private ImageView ivTop;
    private boolean isShowGuessLove = true;
    private View rootView;

    private boolean isUseCache = false;
    private String sexType = ApiClient.SEX_TYPE_BL;
    private String pageType ;
    private HomeTopicPresenter topicPresenter;
    private HomeSpeakerPresenter speakerPresenter;
    private HomeBannerPresenter bannerPresenter;
    private HomeCategoryPresenter categoryPresenter;

    private IListScollerState iListScollerState;
    private PullToRefreshListView.OnScrollYListener onScrollYListener;

    public void setOnScrollYListener(PullToRefreshListView.OnScrollYListener onScrollYListener) {
        this.onScrollYListener = onScrollYListener;
    }

    public void setiListScollerState(IListScollerState iListScollerState) {
        this.iListScollerState = iListScollerState;
    }

    public void setSexType(String sexType) {
        this.sexType = sexType;
    }

    public void setPageType(String pageType) {
        this.pageType = pageType;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_dfnovel, null);
            initPresenter();
            initsView(rootView);
        }
        return rootView;
    }

    private void initPresenter() {
        topicPresenter = new HomeTopicPresenter(getActivity(),pageType,this);
        speakerPresenter = new HomeSpeakerPresenter(getActivity(),this);
        bannerPresenter = new HomeBannerPresenter(getActivity(),pageType,this);
        categoryPresenter = new HomeCategoryPresenter(getActivity(),pageType,this);
    }

    private void loadCache() {
        if (bannerPresenter != null) {
            bannerPresenter.loadBannerCache();
        }
        if (categoryPresenter != null) {
            categoryPresenter.loadCategoryCache(sexType);
        }
        if (speakerPresenter != null) {
            speakerPresenter.loadSpeakerCache(sexType);
        }
        if (topicPresenter != null) {
            topicPresenter.loadTopicCache(sexType);
        }

    }


    private void initsView(View rootView) {
        listView = (PullToRefreshListView) rootView.findViewById(R.id.fragment_dfnovel_list);
        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        listView.setRefreshable(true);
        listView.setOnLoadAndRefreshListener(this);
        listView.setMoveListener(new ToUpOrDownable() {
            @Override
            public void moveToUp() {
                ivTop.setVisibility(View.INVISIBLE);
                setToTopVisible(false);
            }

            @Override
            public void moveToDown(int firstVisiblePos) {
                if (firstVisiblePos > 2) {
                    ivTop.setVisibility(View.VISIBLE);
                    setToTopVisible(true);
                }else{
                    ivTop.setVisibility(View.INVISIBLE);
                }
            }
        });
        listView.setOnScrollStateListent(new PullToRefreshListView.IOnScrollState() {
            @Override
            public void onState(int state) {
                if (iListScollerState != null) {
                    iListScollerState.scrollState(state);
                }
            }
        });
        listView.setOnScrollYListener(onScrollYListener);
        ivTop = (ImageView) rootView.findViewById(R.id.fragment_dfnovel_upTop);
        ivTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToTop();
            }
        });
        initsListViewHeadView();
    }

    public void scrollToTop() {
        if (listView != null) {
            listView.setSelection(0);
        }
        if (ivTop != null) {
            ivTop.setVisibility(View.INVISIBLE);

        }
    }

    public void stopSpeaker() {
        if (autoPollRecyclerView != null) {
            autoPollRecyclerView.stop();
        }
    }

    public void startSpeaker() {
        if (autoPollRecyclerView != null) {
            autoPollRecyclerView.start();
        }
    }

    private boolean toTopButtonIsShow = false;

    private void setToTopVisible(boolean visibleAnimation) {
        ObjectAnimator objectAnimators = null;
        AnimatorSet animatorSet = new AnimatorSet();
        if (visibleAnimation) {
            //出现动画
            if (!toTopButtonIsShow) {
                objectAnimators = ObjectAnimator.ofFloat(ivTop, "translationY", com.taptech.doufu.util.ScreenUtil.getScreenHeight(this.getActivity()), 0);
            }
            toTopButtonIsShow = true;
        } else {
            //隐藏动画
            if (toTopButtonIsShow) {
                objectAnimators = ObjectAnimator.ofFloat(ivTop, "translationY", 0, com.taptech.doufu.util.ScreenUtil.getScreenHeight(this.getActivity()) - ivTop.getMeasuredHeight());
            }
            toTopButtonIsShow = false;
        }
        if (objectAnimators != null) {
            animatorSet.setDuration(500);
            animatorSet.playTogether(objectAnimators);
            animatorSet.start();
        }

    }

    public void updateNow() {
        getDataFromNet();
    }


    /**
     * request server
     */
    private void getDataFromNet() {
        ((WeMediaApplication) (WeMediaApplication.applicationContext)).sexType = sexType;
        isShowGuessLove = true;
        if (listView.getLoadCompleteImg() != null) {
            listView.getLoadCompleteImg().setVisibility(View.GONE);
        }
        listView.setFull(false);
        listView.setLoadmoreable(true);
        if (topicPresenter != null) {
            topicPresenter.getHomeNovelTopic(isUseCache,sexType);
        }
        if (categoryPresenter != null) {
            categoryPresenter.getHomeNovelCategory(isUseCache,sexType);
        }
        if (bannerPresenter != null) {
            bannerPresenter.getHomeNovelBanner(sexType,isUseCache);
        }
        if (speakerPresenter != null) {
            speakerPresenter.getHomeNovelSpeaker(sexType,isUseCache);
        }
    }
    /**
     * 装 headView,包    ，下面
     */
    private void initsListViewHeadView() {
        View homeTopEnterView = LayoutInflater.from(getActivity()).inflate(R.layout.dfhome_viewflow, null);
        llBroadcast =  homeTopEnterView.findViewById(R.id.ll_dfhome_viewflow_broatcast);
        viewFlow =  homeTopEnterView.findViewById(R.id.dfhome_exchange_viewflow);
        viewFlow.setLayoutParams(new RelativeLayout.LayoutParams(ScreenUtil.getScreenWidth(getActivity()), (int) (ScreenUtil.getScreenWidth(getActivity())/1.5)));
        flowIndicator =  homeTopEnterView.findViewById(R.id.dfhome_ad_indicator);
        flowIndicator.setVisibility(View.VISIBLE);

        autoPollRecyclerView = homeTopEnterView.findViewById(R.id.autoPollRecyclerView);
        line_bg =  homeTopEnterView.findViewById(R.id.dfhome_novbel_bg);
        line1 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line1);
        line2 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line2);
        line3 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line3);
        line4 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line4);
        line5 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line5);
        line6 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line6);
        line7 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line7);
        line8 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line8);
        line9 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line9);
        line10 =  homeTopEnterView.findViewById(R.id.dfhome_novel_line10);

        layBottomLine = homeTopEnterView.findViewById(R.id.layBottomLine);

        llAutoLayout =  homeTopEnterView.findViewById(R.id.auto_home_novel_layout);
        noticeImg =  homeTopEnterView.findViewById(R.id.dfBL_novel_notice_img);
        classifyName[0] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv1);
        classifyName[1] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv2);
        classifyName[2] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv3);
        classifyName[3] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv4);
        classifyName[4] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv5);
        classifyName[5] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv6);
        classifyName[6] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv7);
        classifyName[7] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv8);
        classifyName[8] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv9);
        classifyName[9] =  homeTopEnterView.findViewById(R.id.dfhome_novel_tv10);
        classifyImag[0] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv1);
        classifyImag[1] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv2);
        classifyImag[2] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv3);
        classifyImag[3] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv4);
        classifyImag[4] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv5);
        classifyImag[5] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv6);
        classifyImag[6] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv7);
        classifyImag[7] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv8);
        classifyImag[8] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv9);
        classifyImag[9] =  homeTopEnterView.findViewById(R.id.dfhome_novel_iv10);
        listView.addHeaderView(homeTopEnterView);
        contentAdaList = new ArrayList<>();
        listAda = new DfhomeNovelAdapter3(getActivity(), contentAdaList, sexType);
        listView.setAdapter(listAda);
    }

    @Override
    public synchronized void onRefresh() {
        listView.setLoadmoreable(false);
        last = "";
        //重新加载数据
        isUseCache = false;
        getDataFromNet();
    }


    private String lastRequest = "-999999990";

    @Override
    public synchronized void loadMore() {
        if (listView.isLoadmoreable()) {
            isShowGuessLove = false;
            if (!CommonUtils.isNetWorkConnected(getActivity())) {
                listView.loadMoreComplete();
                listView.setLoadmoreable(true);
                return;
            }

            if (!lastRequest.equals(last)) {
                ((WeMediaApplication) (WeMediaApplication.applicationContext)).sexType = sexType;
                getPersonalLike(last);
                lastRequest = last;
            }
        }
    }


    private synchronized void addNewBean(List<DFHomeNovelBeans> tempAdapList) {
        for (int i = 0; i < tempAdapList.size(); i++) {
            DFHomeNovelBeans cateBean = tempAdapList.get(i);
            if (0 == i) {
//                if (isShowGuessLove) {
//                    cateBean.hasTitleA = true;
//                    cateBean.upTitleNameA = "猜你喜欢";
//                    cateBean.isMoreUrlA = false;
//                }
            }
            if (contentAdaList != null) {
                contentAdaList.add(cateBean);
            }
        }
    }


    private void initsDefaultCategoryData() {
        line1.setVisibility(View.GONE);
        line2.setVisibility(View.GONE);
        line3.setVisibility(View.GONE);
        line4.setVisibility(View.GONE);
        line5.setVisibility(View.GONE);
        line6.setVisibility(View.GONE);
        line7.setVisibility(View.GONE);
        line8.setVisibility(View.GONE);
        line9.setVisibility(View.GONE);
        line10.setVisibility(View.GONE);
        layBottomLine.setVisibility(View.GONE);
    }

    private void initsCategoryData(List<DFHomeCategoryBean> tempCategoryList) {
        initsDefaultCategoryData();
        NetworkImageView ivTemp;
        TextView tvTemp;
        RelativeLayout lineTemp;
        if (tempCategoryList.size() == 0) {
            line_bg.setVisibility(View.GONE);
        }else if (tempCategoryList.size() <= 5) {
            for (int i = 0; (i < 5) && (i < tempCategoryList.size()); i++) {
                if (0 == i) {
                    lineTemp = line1;
                    ivTemp = classifyImag[0];
                    tvTemp = classifyName[0];
                } else if (1 == i) {
                    lineTemp = line2;
                    ivTemp = classifyImag[1];
                    tvTemp = classifyName[1];
                } else if (2 == i) {
                    lineTemp = line3;
                    ivTemp = classifyImag[2];
                    tvTemp = classifyName[2];
                } else if (3 == i) {
                    lineTemp = line4;
                    ivTemp = classifyImag[3];
                    tvTemp = classifyName[3];
                } else if (4 == i) {
                    lineTemp = line5;
                    ivTemp = classifyImag[4];
                    tvTemp = classifyName[4];
                } else {
                    lineTemp = null;
                    ivTemp = null;
                    tvTemp = null;
                }

                if (ivTemp != null) {
                    ivTemp.setFullScreen(false);
                    lineTemp.setVisibility(View.VISIBLE);
                    setCategoryClickListener(lineTemp, tempCategoryList.get(i).getType(), i);
                    ivTemp.setURL(tempCategoryList.get(i).getImgurl());
                    tvTemp.setText(tempCategoryList.get(i).getTitle());

                }
            }
        }else{
            layBottomLine.setVisibility(View.VISIBLE);
            for (int i = 0; (i < 10) && (i < tempCategoryList.size()); i++) {
                if (0 == i) {
                    lineTemp = line1;
                    ivTemp = classifyImag[0];
                    tvTemp = classifyName[0];
                } else if (2 == i) {
                    lineTemp = line2;
                    ivTemp = classifyImag[1];
                    tvTemp = classifyName[1];
                } else if (4 == i) {
                    lineTemp = line3;
                    ivTemp = classifyImag[2];
                    tvTemp = classifyName[2];
                } else if (6 == i) {
                    lineTemp = line4;
                    ivTemp = classifyImag[3];
                    tvTemp = classifyName[3];
                } else if (8 == i) {
                    lineTemp = line5;
                    ivTemp = classifyImag[4];
                    tvTemp = classifyName[4];
                } else if (1 == i) {
                    lineTemp = line6;
                    ivTemp = classifyImag[5];
                    tvTemp = classifyName[5];
                } else if (3 == i) {
                    lineTemp = line7;
                    ivTemp = classifyImag[6];
                    tvTemp = classifyName[6];
                }else if (5 == i) {
                    lineTemp = line8;
                    ivTemp = classifyImag[7];
                    tvTemp = classifyName[7];
                }else if (7 == i) {
                    lineTemp = line9;
                    ivTemp = classifyImag[8];
                    tvTemp = classifyName[8];
                }else if (9 == i) {
                    lineTemp = line10;
                    ivTemp = classifyImag[9];
                    tvTemp = classifyName[9];
                }else {
                    lineTemp = null;
                    ivTemp = null;
                    tvTemp = null;
                }

                if (ivTemp != null) {
                    ivTemp.setFullScreen(false);
                    lineTemp.setVisibility(View.VISIBLE);
                    setCategoryClickListener(lineTemp, tempCategoryList.get(i).getType(), i);
                    ivTemp.setURL(tempCategoryList.get(i).getImgurl());
                    tvTemp.setText(tempCategoryList.get(i).getTitle());

                }
            }
        }
    }

    private void setCategoryClickListener(View clickView, final String viewType, final int pos) {
        clickView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sexType == ApiClient.SEX_TYPE_BL) { //彩虹
                    TMAnalysis.event(getActivity(), "MAIN_CAIHONG_"+(pos+1));
                }else{ //言情
                    TMAnalysis.event(getActivity(), "MAIN_YANQING_"+(pos+1));
                }
                goWithCategoryClick(viewType, pos);
            }
        });
    }

    private void goWithCategoryClick(final String viewType, final int pos) {
        ((WeMediaApplication) (WeMediaApplication.applicationContext)).sexType = sexType;

        HashMap<String, String> events = new HashMap<>();
        events.put("title", categoryList.get(pos).getTitle());
        events.put("type",viewType);
        StringBuilder eventName = new StringBuilder();
        if ("1".equals(sexType)) {
            eventName.append("rainbow-");
            TMAnalysis.event(getContext(), UmengEventName.RAINBOW_ENTRANCE,events);
        }else{
            eventName.append("romance-");
            TMAnalysis.event(getContext(), UmengEventName.ROMANCE_ENTRANCE,events);
        }
        eventName.append("entrance");
        eventName.append(categoryList.get(pos).getTitle());
        UserLogManager.addLog(eventName.toString());
        switch (Integer.parseInt(viewType)) {
            case HomeEnterType.CATEGORY://分类
                TMAnalysis.event(getActivity(), "4_1_NOVEL_DM_class");
                Intent intent1 = new Intent();
                intent1.putExtra(RecycleViewLoadType.recycleNeedChangeData, true);
                intent1.putExtra(Constant.URL, categoryList.get(pos).getMoreUrl() + "?");
                intent1.putExtra(Constant.TYPE, 6);
                intent1.putExtra(Constant.TITLE, categoryList.get(pos).getTitle());
                intent1.putExtra(IntentKey.INTENT_KEY_SEX_TYPE,sexType);

                new StartActivityUtils(getActivity(), intent1).toNovelSortActivity();
                break;
            case HomeEnterType.WEEX://weex页面**
                TMAnalysis.event(getActivity(), "4_1_NOVEL_DM_bookreview");
                SimpleWeexActivity.startActivity(getActivity(),categoryList.get(pos).getMoreUrl());
                break;
            case HomeEnterType.VIP://VIP 文
                ChoiceUtils.jumpToMoreNovel(getActivity(),categoryList.get(pos).getTitle(),categoryList.get(pos).getMoreUrl());
                break;
            case HomeEnterType.BOOK_LIST://书单Schem
                TMAnalysis.event(getActivity(), "4_1_NOVEL_DM_booklist");
                Intent intent3 = new Intent();
                intent3.putExtra(Constant.URL, categoryList.get(pos).getMoreUrl() + "?");
                intent3.putExtra(Constant.TYPE, 1);
                intent3.putExtra(Constant.TITLE, categoryList.get(pos).getTitle());
                String helpUrl = "";
                if (sexType == ApiClient.SEX_TYPE_BL) {
                    helpUrl = "http://api.doufu.la/index.php/journal/detail?id=269";
                } else if (sexType == ApiClient.SEX_TYPE_UN_BL) {
                    helpUrl = "http://api.doufu.la/index.php/journal/detail?id=270";
                }
                if (!TextUtils.isEmpty(helpUrl)) {
                    intent3.putExtra(Constant.HELP_URL,helpUrl);
                }
                intent3.putExtra(RecycleViewLoadType.recycleNeedChangeData, false);
                new StartActivityUtils(getActivity(), intent3).toNovelBookListActivity();
                break;
            case HomeEnterType.RANK://排行**
                TMAnalysis.event(getActivity(), "4_1_NOVEL_DM_Ranking");
                Intent intent = new Intent(getActivity(), NovelRankActivity.class);
                intent.putExtra(NovelRankActivity.SEX_TYPE, sexType);
                getActivity().startActivity(intent);
                break;
            case HomeEnterType.MEMBER://
                UserMemberActivity.Companion.startActivity(getActivity());
                break;

            case HomeEnterType.WEB:
                BrowseActivity.startActivity(getActivity(),categoryList.get(pos).getMoreUrl());
                break;
            case HomeEnterType.FINISHED://完本
                Intent intent7 = new Intent();
                intent7.putExtra(Constant.URL, categoryList.get(pos).getMoreUrl());
                new StartActivityUtils(getActivity(), intent7).toNovelMoreWeexActivity();
                break;
            default:
                break;
        }
    }

    private void initFlowData() {

        try {
            if (viewFlow != null && bannerList != null) {
                viewFlowAdapter = new HomeBannerImageAdapter(bannerList, getActivity(),pageType);
                viewFlow.setAdapter(viewFlowAdapter);
                viewFlow.setmSideBuffer(bannerList.size());// 实际图片张数为size大小
                viewFlow.setTimeSpan(5000);// 图片切换时间间隔
                if (viewPager != null) {
                    viewFlow.setViewPage(viewPager);
                }
                flowIndicator.setViewFlow(viewFlow);
                viewFlow.setFlowIndicator(flowIndicator);
                if (bannerList.size() > 1) {
                    viewFlow.startAutoFlowTimer(); // 启动自动播放
                }
            } else {
                viewFlowAdapter.setData(bannerList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setViewPager(TTHomeViewPager mViewPager) {
        this.viewPager = mViewPager;
    }


    private boolean isAttachActivity = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        isAttachActivity = true;
    }

    /**
     * 切换主题
     */
    public void changeThemeWithColorful() {
        if (!isAttachActivity) {
            return;
        }
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            changeDarkViewBg();
        } else {
            changeDayViewBg();
        }
        if (listAda != null) {
            listAda = new DfhomeNovelAdapter3(getActivity(), contentAdaList, sexType);
            listView.setAdapter(listAda);
        }
        if (viewFlowAdapter != null) {
            initFlowData();
        }
        listView.changeBgView();
    }

    private void changeDayViewBg() {
        line_bg.setBackground(getResources().getDrawable(R.color.white));
        for (int i = 0; i < classifyName.length; i++) {
            classifyName[i].setTextColor(getResources().getColor(R.color.text_color_2));
            classifyImag[i].setBackgroundColor(getResources().getColor(R.color.transparent_background));
            classifyImag[i].setAlpha(1.0f);
        }
        noticeImg.setAlpha(1.0f);
        llBroadcast.setBackgroundColor(getResources().getColor(R.color.white));
        llAutoLayout.setBackgroundResource(R.drawable.shape_roundrectangle);
    }

    private void changeDarkViewBg() {
        line_bg.setBackgroundColor(getResources().getColor(R.color.fg_dark));
        for (int i = 0; i < classifyName.length; i++) {
            classifyName[i].setTextColor(getResources().getColor(R.color.text_dark));
            classifyImag[i].setBackgroundColor(getResources().getColor(R.color.fg_dark));
            classifyImag[i].setAlpha(Constant.imagViewAlpha);
        }
        noticeImg.setAlpha(Constant.imagViewAlpha);
        llBroadcast.setBackgroundColor(getResources().getColor(R.color.fg_dark));
        llAutoLayout.setBackgroundResource(R.drawable.shape_roundrectangle_dark);
    }

    private static final String TAG = DFBLNovelFragment.class.getSimpleName();

    private void getPersonalLike(String last) {
        if (HomePageType.RAINBOW_FINISHED.equals(pageType) || HomePageType.COPYRIGHT.equals(pageType)) {
            if (listView != null) {
                listView.getLoadCompleteImg().setImageResource(R.drawable.draw_recommand_bottom_img);
                listView.setFull(true);
                listView.loadMoreComplete();
                listView.setLoadmoreable(false);
                listView.onRefreshComplete();
            }
            return;
        }
        ApiClient.getInstance().setUseCache(isUseCache).setSexType(sexType).getService().getNovelPersonalLike(sexType, last)
                .compose(RxJavaHelper.<PersonalLikeGsonBean>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<PersonalLikeGsonBean>(){
                    @Override
                    public void onNext(PersonalLikeGsonBean bean) {
                        super.onNext(bean);
                        if (bean != null) {
                            updataPersonalLikeUi(bean.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (listView != null) {
                            listView.onRefreshComplete();
                        }
                    }
                });
    }

    private void updataPersonalLikeUi(PersonalLikeBean bean) {
        if (bean == null) {
            return;
        }
        if (listView != null) {
            listView.onRefreshComplete();
        }
        List<DFHomeNovelBeans> tempAdapList = bean.getList();
        if ((tempAdapList != null) && (tempAdapList.size() > 0)) {
            addNewBean(tempAdapList);
        } else {
            listView.getLoadCompleteImg().setImageResource(R.drawable.draw_recommand_bottom_img);
            listView.setFull(true);
            listView.loadMoreComplete();
            listView.setLoadmoreable(false);
        }
        if (!TextUtils.isEmpty(bean.getLast())) {
            String templast = bean.getLast();
            if ((templast != null) && (templast.length() > 0) && (!last.equals(templast))) {
                listView.setLoadmoreable(true);
                last = templast;
            } else {
                listView.getLoadCompleteImg().setImageResource(R.drawable.draw_recommand_bottom_img);
                listView.setFull(true);
                listView.loadMoreComplete();
                listView.setLoadmoreable(false);
            }
        }

        listAda.refrushList(contentAdaList);
    }


    @Override
    public void fetchData() {
        loadCache();
        getDataFromNet();
    }

    @Override
    public void onTopicNext(List<Object> list,boolean isCache) {
        contentAdaList = list;
        if (listAda != null && isCache) {
            listAda.refrushList(contentAdaList);
        }else{
            listAda = new DfhomeNovelAdapter3(getActivity(), contentAdaList, sexType);
            listView.setAdapter(listAda);
        }
        if (!isCache) {
            getPersonalLike("");
        }
    }

    @Override
    public void onTopicError() {
        getPersonalLike("");
    }

    @Override
    public void onSpeakerNext(List<MarketNovelNoticeBean> list, boolean isCache) {
        if (list != null) {
            listNotice = list;
            if (listNotice != null && listNotice.size() > 0) {
                if (!TextUtils.isEmpty(listNotice.get(0).getMsg())) {
                    llBroadcast.setVisibility(View.VISIBLE);
                    AutoPollAdapter adapter = new AutoPollAdapter(getActivity(), list,pageType);
                    autoPollRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                    autoPollRecyclerView.setAdapter(adapter);
                    autoPollRecyclerView.start();
                } else {
                    llBroadcast.setVisibility(View.GONE);
                }
            } else {
                llBroadcast.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onBannerNext(List<DFHomeBannerBean> list, boolean isCache) {
        if (list != null) {
            bannerList = list;
            initFlowData();
        }
    }

    @Override
    public void onCategoryNext(List<DFHomeCategoryBean> list, boolean isCache) {
        if (list != null) {
            categoryList = list;
            initsCategoryData(categoryList);
        }
    }
}
